package com.sdp.wz.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wz.db.wz_warehouse_right.entity.WzWarehouseRight;
import com.sdp.wz.db.wz_warehouse_right.service.IWzWarehouseRightService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/WzWarehouseRightController")
public class WzWarehouseRightController {

	@Autowired
	private IWzWarehouseRightService iWzWarehouseRightService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWzWarehouseRightService, WzWarehouseRight.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WzWarehouseRight warehouseRight) {
		if(warehouseRight==null) {
			return Result.error("库管信息为空");
		}
		if(StringUtils.isEmpty(warehouseRight.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWzWarehouseRightService, WzWarehouseRight.class, new String[] {"empNo","warehouseCode"}, "keyId", warehouseRight, null, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("同一个人在同一仓库的权限不能重复");
			}
			iWzWarehouseRightService.save(warehouseRight);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWzWarehouseRightService, WzWarehouseRight.class, new String[] {"empNo","warehouseCode"}, "keyId", null, warehouseRight, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("同一个人在同一仓库的权限不能重复");
			}
			iWzWarehouseRightService.updateById(warehouseRight);
		}
	
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWzWarehouseRightService.removeById(keyId);
		return Result.ok();
	}
}

